import random
import string
from tkinter import *
from tkinter import colorchooser
from basic import Line, Ellipse, Rectangle, Triangle, Round
from shape import Shape
from originator import Originator

TAG_LEN = 4
SHOW_LOG = True


def randstr(num):
    salt = ''.join(random.sample(string.ascii_letters + string.digits, num))
    return salt


def show_shape_list(shape_list):
    for shape in shape_list:
        for basic in shape.basic_list:
            if basic.shape != 'triangle':
                print(basic.id, basic.tag, basic.shape, basic.left_x,
                      basic.left_y,
                      basic.right_x,
                      basic.right_y)
            else:
                print(basic.id, basic.tag, basic.shape, basic.left_down_x,
                      basic.left_down_y,
                      basic.right_down_x,
                      basic.right_down_y,
                      basic.up_x,
                      basic.up_y)


class MyCanvas(Canvas):
    def __init__(self, master):
        self.master = master
        self.shape_list = []
        self.color_list = ['', 'red', 'yellow', 'blue', 'green']
        self.init_canvas()
        self.init_menu()
        self.init_label()
        self.reset()
        self.init_bind()

    def init_canvas(self):
        self.cv = Canvas(root, background='pink')
        self.cv.pack(fill=BOTH, expand=True)
        self.originator = Originator(self.cv)

    def init_menu(self):
        self.menubar = Menu(self.master)
        self.master['menu'] = self.menubar
        file_menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label='文件', menu=file_menu)
        file_menu.add_command(label="保存", command=self.on_save)
        file_menu.add_command(label="撤销", command=lambda i=7: self.choose_move(i))
        file_menu.add_command(label="退出", command=self.on_exit)

        lang_menu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label='图形', menu=lang_menu)
        lang_menu.add_command(label="直线", command=lambda i=0: self.choose(i))
        lang_menu.add_command(label="矩形", command=lambda i=1: self.choose(i))
        lang_menu.add_command(label="椭圆", command=lambda i=2: self.choose(i))
        lang_menu.add_command(label="圆", command=lambda i=4: self.choose(i))
        lang_menu.add_command(label="三角形", command=lambda i=3: self.choose(i))

        lang_menu1 = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label='选择', menu=lang_menu1)
        lang_menu1.add_command(label="新建", command=lambda i=1: self.choose_move(i))
        lang_menu1.add_command(label="移动", command=lambda i=2: self.choose_move(i))
        lang_menu1.add_command(label="添加文字描述", command=lambda i=4: self.choose_move(i))
        lang_menu1.add_command(label="查看文字描述", command=lambda i=3: self.choose_move(i))
        lang_menu1.add_command(label="复制", command=lambda i=5: self.choose_move(i))
        lang_menu1.add_command(label="粘贴", command=lambda i=6: self.choose_move(i))

        lang_menu2 = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label='配置', menu=lang_menu2)
        lang_menu2.add_command(label="改变颜色", command=lambda i=8: self.choose_move(i))
        lang_menu2.add_command(label="放大图形", command=lambda i=9: self.choose_move(i))
        lang_menu2.add_command(label="缩小图形", command=lambda i=10: self.choose_move(i))

    def choose_move(self, i):
        if i == 1:  # 新建
            self.flag = 1
        if i == 2:  # 移动
            self.flag = 2
        elif i == 3:  # 查看文字描述
            self.flag = 3
        elif i == 4:  # 添加文字描述
            self.flag = 4
        elif i == 5:  # 复制
            self.flag = 5
        elif i == 6:  # 粘贴
            self.flag = 6
        elif i == 7:  # 撤销
            self.flag = 7
        elif i == 8:  # 变色
            self.flag = 8
        elif i == 9:  # 放大
            self.flag = 9
        elif i == 10:  # 缩小
            self.flag = 10
        self.init_bind()

    def on_exit(self):
        self.master.destroy()

    def on_save(self):
        # if SHOW_LOG:
        #     print('\nbefore')
        #     print('shape_list:')
        #     show_shape_list(self.shape_list)
        #     print('old_shape_list')
        #     show_shape_list(self.originator.old_shape_list)
        self.originator.changeStatus(self.shape_list)
        # if SHOW_LOG:
        #     print('\nafter')
        #     print('shape_list:')
        #     show_shape_list(self.shape_list)
        #     print('old_shape_list')
        #     show_shape_list(self.originator.old_shape_list)

    def init_label(self):
        self.label1 = Label(self.master, text='当前选择的图形为：', relief='sunken', justify='left', anchor=W)
        self.label1.pack(side=LEFT, fill=X, expand=Y)
        self.label2 = Label(self.master, text='当前鼠标的位置为：', padx=1, relief='sunken')
        self.label2.pack(side=RIGHT)

    def txt(self, event):
        self.label2.config(text='当前鼠标的位置为：(%s,%s)' % (event.x, event.y))
        txt = ''
        if self.item_type == 0:
            txt = '直线'
        if self.item_type == 1:
            txt = '矩形'
        if self.item_type == 2:
            txt = '椭圆'
        if self.item_type == 3:
            txt = '三角形'
        if self.item_type == 3:
            txt = '圆形'
        if self.flag == 1:
            txt2 = '新建'
        if self.flag == 2:
            txt2 = '修改'
        if self.flag == 3:
            txt2 = '查看txt'
        if self.flag == 4:
            txt2 = '添加txt'
        if self.flag == 5:
            txt2 = '复制'
        if self.flag == 6:
            txt2 = '粘贴'
        if self.flag == 7:
            txt2 = '撤销'
        if self.flag == 8:
            txt2 = '变色'
        if self.flag == 9:
            txt2 = '放大'
        if self.flag == 10:
            txt2 = '缩小'
        local = (self.first_x, self.first_y, self.last_x, self.last_y)
        result = '当前的状态为：%s，选择的图形为：%s%s' % (txt2, txt, local)
        self.label1.config(text=result)

    def getShape(self, event):
        mouse_x = event.x
        mouse_y = event.y
        for shape in self.shape_list:
            for basic in shape.basic_list:
                if basic.shape == 'line':  # 直线
                    if basic.left_x <= mouse_x <= basic.right_x and basic.left_y <= mouse_y <= basic.right_y:
                        if SHOW_LOG:
                            print(shape.basic_list[0].id)
                        return shape
                if basic.shape == 'rectangle':  # 矩形
                    if basic.left_x <= mouse_x <= basic.right_x and basic.left_y <= mouse_y <= basic.right_y:
                        if SHOW_LOG:
                            print(shape.basic_list[0].id)
                        return shape
                if basic.shape == 'ellipse':  # 椭圆
                    if basic.left_x <= mouse_x <= basic.right_x and basic.left_y <= mouse_y <= basic.right_y:
                        if SHOW_LOG:
                            print(shape.basic_list[0].id)
                        return shape
                if basic.shape == 'round':  # 圆
                    if basic.left_x <= mouse_x <= basic.right_x and basic.left_y <= mouse_y <= basic.right_y:
                        if SHOW_LOG:
                            print(shape.basic_list[0].id)
                        return shape
                if basic.shape == 'triangle':  # 三角形
                    if SHOW_LOG:
                        print(basic.left_down_x, basic.right_down_x, mouse_x)
                        print(basic.left_down_y, basic.up_y, mouse_y)
                    if basic.left_down_x <= mouse_x <= basic.right_down_x and \
                            basic.up_y <= mouse_y <= basic.right_down_y:
                        if SHOW_LOG:
                            print(shape.basic_list[0].id)
                        return shape
        return None

    def getShape_notself(self, event, mytag):  # 得到非自己的那个shape
        mouse_x = event.x
        mouse_y = event.y
        for shape in self.shape_list:
            if shape.shape_tag != mytag:  # 不能是自己
                for basic in shape.basic_list:
                    if basic.shape == 'line':  # 直线
                        if basic.left_x <= mouse_x <= basic.right_x and basic.left_y <= mouse_y <= basic.right_y:
                            if SHOW_LOG:
                                print(shape.basic_list[0].id)
                            return shape
                    if basic.shape == 'rectangle':  # 矩形
                        if basic.left_x <= mouse_x <= basic.right_x and basic.left_y <= mouse_y <= basic.right_y:
                            if SHOW_LOG:
                                print(shape.basic_list[0].id)
                            return shape
                    if basic.shape == 'ellipse':  # 椭圆
                        if basic.left_x <= mouse_x <= basic.right_x and basic.left_y <= mouse_y <= basic.right_y:
                            if SHOW_LOG:
                                print(shape.basic_list[0].id)
                            return shape
                    if basic.shape == 'round':  # 圆
                        if basic.left_x <= mouse_x <= basic.right_x and basic.left_y <= mouse_y <= basic.right_y:
                            if SHOW_LOG:
                                print(shape.basic_list[0].id)
                            return shape
                    if basic.shape == 'triangle':  # 三角形
                        if SHOW_LOG:
                            print(basic.left_down_x, basic.right_down_x, mouse_x)
                            print(basic.left_down_y, basic.up_y, mouse_y)
                        if basic.left_down_x <= mouse_x <= basic.right_down_x and \
                                basic.up_y <= mouse_y <= basic.right_down_y:
                            if SHOW_LOG:
                                print(shape.basic_list[0].id)
                            return shape
        return None

    def init_bind(self):
        self.cv.bind("<Motion>", self.mouse_move)
        self.cv.bind("<Motion>", self.txt)
        if self.flag == 1:  # 新建
            if SHOW_LOG:
                print('新建')
            self.cv.bind("<ButtonPress-1>", self.StartCreate)  # 监听左键按下操作响应函数
            self.cv.bind("<ButtonRelease-1>", self.StopCreate)  # 监听左键松开操作响应函数
            self.cv.bind("<B1-Motion>", self.OnMotion)  # 监听鼠标移动操作响应函数
        elif self.flag == 2:  # 移动
            if SHOW_LOG:
                print('移动')
            self.cv.bind("<ButtonPress-1>", self.StartMove)  # 监听左键按下操作响应函数
            self.cv.bind("<ButtonRelease-1>", self.StopMove)  # 监听左键松开操作响应函数
            self.cv.bind("<B1-Motion>", self.OnMove)  # 监听鼠标移动操作响应函数
        elif self.flag == 3:  # 查看txt
            if SHOW_LOG:
                print('查看txt')
            self.cv.bind("<ButtonRelease-1>", self.showTxt)
        elif self.flag == 4:  # 添加txt
            if SHOW_LOG:
                print('添加txt')
            self.cv.bind("<ButtonRelease-1>", self.addTxt)
        elif self.flag == 5:  # 复制
            if SHOW_LOG:
                print('复制')
            self.cv.bind("<ButtonRelease-1>", self.copy)
        elif self.flag == 6:  # 粘贴
            if SHOW_LOG:
                print('粘贴')
            self.cv.bind("<ButtonRelease-1>", self.paste)
        elif self.flag == 7:  # 撤销
            if SHOW_LOG:
                print('撤销')
            self.revocation()
        elif self.flag == 8:  # 变色
            if SHOW_LOG:
                print('变色')
            self.cv.bind("<ButtonRelease-1>", self.changeColor)
        elif self.flag == 9:  # 放大
            if SHOW_LOG:
                print('放大')
            self.cv.bind("<ButtonRelease-1>", self.largen)
        elif self.flag == 10:  # 缩小
            if SHOW_LOG:
                print('缩小')
            self.cv.bind("<ButtonRelease-1>", self.lessen)

    def largen(self, event):
        temp_shape = self.getShape(event)
        if temp_shape is None:
            self.reset()
            return
        temp_shape.largenShape()
        self.reset()

    def lessen(self, event):
        temp_shape = self.getShape(event)
        if temp_shape is None:
            self.reset()
            return
        temp_shape.lessenShape()
        self.reset()

    def changeColor(self, event):
        temp_shape = self.getShape(event)
        if temp_shape is None:
            self.reset()
            return
        temp_shape.changeColor()
        self.reset()

    def renew_shape_list(self, old_shape_list):
        self.shape_list.clear()
        for shape in old_shape_list:
            new_shape = Shape(self.cv, randstr(TAG_LEN), shape.color)
            if SHOW_LOG:
                print(shape.color)
            for txt in shape.txt_list:
                new_shape.txt_list.append(txt)
            for basic in shape.basic_list:
                if basic.shape == 'line':
                    coords = [basic.left_x, basic.left_y, basic.right_x, basic.right_y]
                    new_basic = Line(basic.tag, basic.id, 'line', coords)
                    new_shape.addBasic(new_basic)
                elif basic.shape == 'rectangle':
                    coords = [basic.left_x, basic.left_y, basic.right_x, basic.right_y]
                    new_basic = Rectangle(basic.tag, basic.id, 'rectangle', coords)
                    new_shape.addBasic(new_basic)
                elif basic.shape == 'ellipse':
                    coords = [basic.left_x, basic.left_y, basic.right_x, basic.right_y]
                    new_basic = Ellipse(basic.tag, basic.id, 'ellipse', coords)
                    new_shape.addBasic(new_basic)
                elif basic.shape == 'round':
                    coords = [basic.left_x, basic.left_y, basic.right_x, basic.right_y]
                    new_basic = Round(basic.tag, basic.id, 'round', coords)
                    new_shape.addBasic(new_basic)
                elif basic.shape == 'triangle':
                    coords = [basic.left_down_x, basic.left_down_y, basic.right_down_x, basic.right_down_y, basic.up_x,
                              basic.up_y]
                    new_basic = Triangle(basic.tag, basic.id, 'triangle', coords)
                    new_shape.addBasic(new_basic)
            self.shape_list.append(new_shape)

    def revocation(self):
        # if SHOW_LOG:
        #     print('\nbefore撤销')
        #     print('shape_list:')
        #     show_shape_list(self.shape_list)
        #     print('old_shape_list')
        #     show_shape_list(self.originator.old_shape_list)
        for shape in self.shape_list:
            for basic in shape.basic_list:
                self.cv.delete(basic.id)
        self.renew_shape_list(self.originator.old_shape_list)
        # if SHOW_LOG:
        #     print('\nafter撤销')
        #     print('shape_list:')
        #     show_shape_list(self.shape_list)
        #     print('old_shape_list')
        #     show_shape_list(self.originator.old_shape_list)
        for shape in self.shape_list:
            shape.createShape()
        self.on_save()
        self.reset()

    def mouse_move(self, event):
        self.label2.config(text='当前鼠标的位置为：(%s,%s)' % (event.x, event.y))
        # print('鼠标的坐标为：({}, {})'.format(event.x, event.y))

    def reset(self):
        # 当前选择的图形项
        self.temp_item = None
        self.first_x = None
        self.first_y = None
        self.last_x = None
        self.last_y = None
        self.status = True
        self.temp_shape = None
        self.item_type = 1  # 默认画矩形
        self.flag = 1  # 新建为1，修改为2，删除为3
        self.init_bind()

    def reset_copy(self):
        self.copy_shape = None
        self.copy_first_x = None
        self.copy_first_y = None

    def addTxt(self, event):
        self.temp_shape = self.getShape(event)
        if self.temp_shape is None:
            self.reset()
            return

        def add2Shape():
            if SHOW_LOG:
                print(new_txt.get())
            self.temp_shape.addTxt(new_txt.get())
            window_txt.destroy()
            if SHOW_LOG:
                print(self.temp_shape.getTxt())
            self.reset()

        # 输入窗口
        window_txt = Toplevel(root)
        window_txt.geometry('200x200')
        window_txt.title('添加文字描述')
        new_txt = StringVar()
        entry_new_txt = Entry(window_txt, textvariable=new_txt)
        entry_new_txt.place(x=5, y=10)
        btn_finish = Button(window_txt, text='确认', command=add2Shape)
        btn_finish.place(x=100, y=150)

    def showTxt(self, event):
        temp_shape = self.getShape(event)
        if temp_shape is None:
            self.reset()
            return

        def closeWin():
            window_txt.destroy()
            self.reset()

        # 小窗口
        window_txt = Toplevel(root)
        window_txt.geometry('200x230')
        window_txt.title('文字描述')
        show_list = Listbox(window_txt)
        if len(temp_shape.txt_list) == 0:
            show_list.insert(0, '空')
        for item in temp_shape.txt_list:
            show_list.insert(0, item)
        show_list.place(x=5, y=10)
        btn_finish = Button(window_txt, text='确认', command=closeWin)
        btn_finish.place(x=80, y=200)

    def shapePaste(self, shape, event):
        if SHOW_LOG:
            print('shape_Paste')
        moved_x = self.last_x - self.copy_first_x
        moved_y = self.last_y - self.copy_first_y
        new_shape = Shape(self.cv, randstr(TAG_LEN), shape.color)
        for txt in shape.txt_list:
            new_shape.txt_list.append(txt)
        for basic in shape.basic_list:
            if basic.shape == 'line':
                new_tag = randstr(TAG_LEN)
                new_item = self.cv.create_line(basic.left_x + moved_x, basic.left_y + moved_y,
                                               basic.right_x + moved_x, basic.right_y + moved_y,
                                               tags=new_tag, fill=self.color_list[new_shape.color % 5])
                new_basic = Line(new_tag, new_item, 'line', self.cv.coords(new_tag))
                new_shape.addBasic(new_basic)
            elif basic.shape == 'rectangle':
                new_tag = randstr(TAG_LEN)
                new_item = self.cv.create_rectangle(basic.left_x + moved_x, basic.left_y + moved_y,
                                                    basic.right_x + moved_x, basic.right_y + moved_y,
                                                    tags=new_tag, fill=self.color_list[new_shape.color % 5])
                new_basic = Rectangle(new_tag, new_item, 'rectangle', self.cv.coords(new_tag))
                new_shape.addBasic(new_basic)
            elif basic.shape == 'ellipse':
                new_tag = randstr(TAG_LEN)
                new_item = self.cv.create_oval(basic.left_x + moved_x, basic.left_y + moved_y,
                                               basic.right_x + moved_x, basic.right_y + moved_y,
                                               tags=new_tag, fill=self.color_list[new_shape.color % 5])
                new_basic = Ellipse(new_tag, new_item, 'ellipse', self.cv.coords(new_tag))
                new_shape.addBasic(new_basic)
            elif basic.shape == 'round':
                new_tag = randstr(TAG_LEN)
                new_item = self.cv.create_oval(basic.left_x + moved_x, basic.left_y + moved_y,
                                               basic.right_x + moved_x, basic.right_y + moved_y,
                                               tags=new_tag, fill=self.color_list[new_shape.color % 5])
                new_basic = Round(new_tag, new_item, 'round', self.cv.coords(new_tag))
                new_shape.addBasic(new_basic)
            elif basic.shape == 'triangle':
                new_tag = randstr(TAG_LEN)
                new_item = self.cv.create_polygon(basic.left_down_x + moved_x,
                                                  basic.left_down_y + moved_y,
                                                  basic.right_down_x + moved_x,
                                                  basic.right_down_y + moved_y,
                                                  basic.up_x + moved_x,
                                                  basic.up_y + moved_y,
                                                  outline="black",
                                                  tags=new_tag,
                                                  fill=self.color_list[new_shape.color % 5])
                new_basic = Triangle(new_tag, new_item, 'triangle', self.cv.coords(new_tag))
                new_shape.addBasic(new_basic)
        return new_shape

    def paste(self, event):
        if self.copy_shape is None:
            self.reset()
            return
        print('paste')
        self.last_x = event.x
        self.last_y = event.y
        if SHOW_LOG is True:
            print(self.copy_shape.color)
        new_shape = self.shapePaste(self.copy_shape, event)
        self.shape_list.append(new_shape)
        self.reset()
        self.reset_copy()

    def copy(self, event):
        self.copy_shape = self.getShape(event)
        print(self.copy_shape)
        if self.copy_shape is None:
            self.reset()
            return
        self.copy_first_x = event.x
        self.copy_first_y = event.y
        self.reset()

    def StartMove(self, event):
        self.temp_shape = self.getShape(event)
        if self.temp_shape is None:
            self.reset()
            return
        self.first_x = event.x
        self.first_y = event.y

    def StopMove(self, event):
        if self.temp_shape is None:
            return
        self.last_x = event.x
        self.last_y = event.y
        self.move_shape(event, self.first_x, self.first_y)
        self.first_x = self.last_x
        self.first_y = self.last_y
        end_shape = self.getShape_notself(event, self.temp_shape.shape_tag)
        if end_shape is None:
            print('stop move')
        else:  # 合并
            print(self.temp_shape.basic_list)
            print(self.temp_shape.basic_list[0].id)
            print(end_shape.basic_list)
            print(end_shape.basic_list[0].id)
            self.temp_shape.mergeShape(end_shape)
            self.shape_list.pop(self.shape_list.index(end_shape))
        self.reset()

    def OnMove(self, event):
        if self.temp_shape is None:
            return
        self.last_x = event.x
        self.last_y = event.y
        self.move_shape(event, self.first_x, self.first_y)
        self.first_x = self.last_x
        self.first_y = self.last_y

    def move_shape(self, event, first_x, first_y):
        self.temp_shape.moveShape(event, first_x, first_y)

    def StartCreate(self, event):
        if not self.status:
            self.reset()
            return
        self.first_x = event.x
        self.first_y = event.y

    def StopCreate(self, event):
        if not self.status:
            return
        if self.temp_item is not None:
            self.cv.delete(self.temp_item)
        self.last_x = event.x
        self.last_y = event.y
        self.drag_handler(event, 1)  # 结束后生成实心图形
        self.reset()

    def OnMotion(self, event):
        if not self.status:
            return
        if self.temp_item is not None:
            self.cv.delete(self.temp_item)
        self.last_x = event.x
        self.last_y = event.y
        self.drag_handler(event, 0)  # 不结束后生成虚心图形

    def choose(self, i):
        if i == 0:
            self.item_type = 0  # 直线
            self.flag = 1  # 默认为新建图形
        elif i == 1:
            self.item_type = 1  # 默认画矩形
            self.flag = 1  # 默认为新建图形
        elif i == 2:
            self.item_type = 2  # 椭圆
            self.flag = 1  # 默认为新建图形
        elif i == 3:
            self.item_type = 3  # 三角形
            self.flag = 1  # 默认为新建图形
        elif i == 4:
            self.item_type = 4  # 圆
            self.flag = 1  # 默认为新建图形

    def drag_handler(self, event, end):
        # 直线
        if self.item_type == 0:
            # 如果第一个点不存在（self.first_x 和 self.first_y都小于0）
            if self.first_x < -1 and self.first_y < -1:
                self.first_x, self.first_y = event.x, event.y
            # 删除上一次绘制的虚线图形
            if self.temp_item is not None:
                self.cv.delete(self.temp_item)
            # 重新绘制虚线
            if end == 0:
                self.temp_item = self.cv.create_line(self.first_x, self.first_y, event.x, event.y, dash=2)
            else:
                tag = randstr(TAG_LEN)
                self.temp_item = self.cv.create_line(self.first_x, self.first_y, event.x, event.y
                                                     , tags=tag)
                basic = Line(tag, self.temp_item, 'line', self.cv.coords(tag))
                shape = Shape(self.cv, tag)
                shape.addBasic(basic)
                self.shape_list.append(shape)
                if SHOW_LOG:
                    print(tag, self.temp_item)
                    print(self.cv.coords(tag))
        # 矩形
        elif self.item_type == 1:
            # 如果第一个点不存在（self.first_x 和 self.first_y都小于0）
            if self.first_x < -1 and self.first_y < -1:
                self.first_x, self.first_y = event.x, event.y
            # 删除上一次绘制的虚线图形
            if self.temp_item is not None:
                self.cv.delete(self.temp_item)
            left_x, left_y = min(self.first_x, event.x), min(self.first_y, event.y)
            right_x, right_y = max(self.first_x, event.x), max(self.first_y, event.y)
            # 重新绘制虚线选择框
            if end == 0:
                self.temp_item = self.cv.create_rectangle(left_x, left_y, right_x, right_y, dash=2)
            else:
                tag = randstr(TAG_LEN)
                self.temp_item = self.cv.create_rectangle(left_x, left_y, right_x, right_y, tags=tag)
                basic = Rectangle(tag, self.temp_item, 'rectangle', self.cv.coords(tag))
                shape = Shape(self.cv, tag)
                shape.addBasic(basic)
                self.shape_list.append(shape)
                if SHOW_LOG:
                    print(tag, self.temp_item)
                    print(self.cv.coords(tag))
        # 椭圆
        elif self.item_type == 2:
            # 如果第一个点不存在（self.first_x 和 self.first_y都小于0）
            if self.first_x < -1 and self.first_y < -1:
                self.first_x, self.first_y = event.x, event.y
            # 删除上一次绘制的虚线图形
            if self.temp_item is not None:
                self.cv.delete(self.temp_item)
            left_x, left_y = min(self.first_x, event.x), min(self.first_y, event.y)
            right_x, right_y = max(self.first_x, event.x), max(self.first_y, event.y)
            # 重新绘制虚线选择框
            if end == 0:
                self.temp_item = self.cv.create_oval(left_x, left_y, right_x, right_y, dash=2)
            else:
                tag = randstr(TAG_LEN)
                self.temp_item = self.cv.create_oval(left_x, left_y, right_x, right_y, tags=tag)
                basic = Ellipse(tag, self.temp_item, 'ellipse', self.cv.coords(tag))
                shape = Shape(self.cv, tag)
                shape.addBasic(basic)
                self.shape_list.append(shape)
                if SHOW_LOG:
                    print(tag, self.temp_item)
                    print(self.cv.coords(tag))
        # 三角形
        elif self.item_type == 3:
            # self.draw_polygon = True
            # 如果第一个点不存在（self.first_x 和 self.first_y都小于0）
            if self.first_x < -1 and self.first_y < -1:
                self.first_x, self.first_y = event.x, event.y
            # 删除上一次绘制的虚线图形
            if self.temp_item is not None:
                self.cv.delete(self.temp_item)
            left_x, left_y = min(self.first_x, event.x), min(self.first_y, event.y)
            right_x, right_y = max(self.first_x, event.x), max(self.first_y, event.y)
            # 重新绘制虚线选择框
            if end == 0:
                self.temp_item = self.cv.create_polygon(left_x, right_y, right_x, right_y, (left_x + right_x) / 2,
                                                        left_y, dash=2, fill="", outline="black")
            else:
                tag = randstr(TAG_LEN)
                self.temp_item = self.cv.create_polygon(left_x, right_y, right_x, right_y, (left_x + right_x) / 2,
                                                        left_y, fill="", outline="black", tags=tag)
                basic = Triangle(tag, self.temp_item, 'triangle', self.cv.coords(tag))
                shape = Shape(self.cv, tag)
                shape.addBasic(basic)
                self.shape_list.append(shape)
                if SHOW_LOG:
                    print(tag, self.temp_item)
                    print(self.cv.coords(tag))
        # 圆
        elif self.item_type == 4:
            # 如果第一个点不存在（self.first_x 和 self.first_y都小于0）
            if self.first_x < -1 and self.first_y < -1:
                self.first_x, self.first_y = event.x, event.y
            # 删除上一次绘制的虚线图形
            if self.temp_item is not None:
                self.cv.delete(self.temp_item)
            left_x, left_y = min(self.first_x, event.x), min(self.first_y, event.y)
            right_x, right_y = max(self.first_x, event.x), max(self.first_y, event.y)
            diameter = min(abs(left_x - right_x), abs(left_y - right_y))
            # 重新绘制虚线选择框
            if end == 0:
                self.temp_item = self.cv.create_oval(left_x, left_y,
                                                     left_x + diameter, left_y + diameter,
                                                     dash=2)
            else:
                tag = randstr(TAG_LEN)
                self.temp_item = self.cv.create_oval(left_x, left_y,
                                                     left_x + diameter, left_y + diameter,
                                                     tags=tag)
                print(self.cv.coords(tag))
                basic = Round(tag, self.temp_item, 'round', self.cv.coords(tag))
                shape = Shape(self.cv, tag)
                shape.addBasic(basic)
                self.shape_list.append(shape)
                if SHOW_LOG:
                    print(tag, self.temp_item)
                    print(self.cv.coords(tag))


if __name__ == "__main__":
    root = Tk()
    root.title('绘图软件')
    root.geometry('700x480')
    mycanvas = MyCanvas(root)

    root.mainloop()
